<?php
date_default_timezone_set("UTC");
/**
 * request class
 *
 * @author K.Tamura <tamura@terras.co.jp>
 */
class tsRequest
{

    public $_requests = array();

    public function __construct()
    {
        self::parse();
    }

    /**
     * return this class instance
     *
     * @param string $file
     * @return object
     */
    public static function getInstance()
    {
        static $instance;
        if( null === $instance )
        {
            $instance = new self();
        }
        return $instance;
    }

    /**
     * parse
     *
     * @param string $file
     * @return void
     */
    private function parse()
    {
        // get requests
        //
        $_requests = array();
        if( true === is_array( $_GET ) )
        {
            foreach( $_GET as $key => $val )
            {
                $_requests[$key] = $val;
            }
        }
        if( true === is_array( $_POST ) )
        {
            foreach( $_POST as $key => $val )
            {
                $_requests[$key] = $val;
            }
        }

        // set requests
        //
        // @todo convert encoding
        $this->_requests = $_requests;
    }

    /**
     * return value
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function get( $key, $default = null )
    {
        if( true === isset( $this->_requests[$key] ) )
        {
            return $this->_requests[$key];
        }
        else
        {
            return $default;
        }
    }

    /**
     * return all values
     *
     * @return array
     */
    public function &getAll()
    {
        return $this->_requests;
    }

    /**
     * has variable
     *
     * @param string $key
     * @return booleam
     */
    public function has( $key = null )
    {
        return ( null !== $this->get( $key ) ) ? true : false;
    }

}
